package com.honeycomb.article.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.honeycomb.article.PageView;
import com.honeycomb.article.PositionScope;
import com.honeycomb.article.SearchPattern;
import com.honeycomb.article.dto.ArticleCatagory;
import com.honeycomb.article.dto.ArticleDetailMessageDto;
import com.honeycomb.article.dto.ArticleinfoDto;
import com.honeycomb.article.service.ArticleService;
import com.honeycomb.common.base.service.impl.BaseServiceImpl;
import com.honeycomb.db.dao.ArticleinfoDao;
import com.honeycomb.utils.MyDateDTTM;

/**
 * 文章Service实现
 * 
 * @author Wangyu
 */
@Service
public class ArticleServiceImpl extends BaseServiceImpl implements ArticleService {
	
	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private ArticleinfoDao articleinfoDao;
	
	@Resource
	private Cache dbDataCache; 

	@Override
	public PageView<ArticleinfoDto> searchArticles(ArticleCatagory catagory, SearchPattern pattern, PositionScope positionScope,
									PageView<ArticleinfoDto> pageView) {
		
		List<ArticleinfoDto> result = null;
		Element cachedElem = dbDataCache.get(pageView.getID() );
		
		if (cachedElem != null) {
			result = (List<ArticleinfoDto>)cachedElem.getObjectValue();
			if (logger.isDebugEnabled()) {
				logger.debug("Get cache content for pgView " + pageView.getID() + " , fetch the data from DB. " + result.size() + " data fetched.");
			}
		}
		else { //缓存不存在
			// 数据库获取的数据量比pageView的最大容纳值大一，以判断当前pageView之后，是否还有数据需要显示。
			result = articleinfoDao.searchArticles(catagory, positionScope, pattern, pageView.getFirstDataRef(), pageView.getMaxAllowedDataCount()+1 );
			
			if (logger.isDebugEnabled()) {
				logger.debug("Cache content is not found for pgView " + pageView.getID() + " , fetch the data from DB. " + result.size() + " data fetched.");
			}
			
			if (result.size() > 0) {
				dbDataCache.put(new Element(pageView.getID(), result) );
				if (logger.isDebugEnabled()) {
					logger.debug("Cache content is set for pgView " + pageView.getID() + " , data size is " + result.size() );
				}
			}
		}
		
		pageView.loadData(result);
		return pageView;
	}

	@Override
	public PageView<ArticleDetailMessageDto> getDetailMessagesOfArticle(Long articleId, SearchPattern searchPattern, PageView<ArticleDetailMessageDto> pageView) {
		List<ArticleDetailMessageDto> result = null;
		Element cachedElem = dbDataCache.get(pageView.getID() );
		
		if (cachedElem != null) {
			result = (List<ArticleDetailMessageDto>)cachedElem.getObjectValue();
			if (logger.isDebugEnabled()) {
				logger.debug("Cache content for pgView " + pageView.getID() + " is fetched. " + result.size() + " data fetched.");
			}
		}
		else { //缓存不存在
			// 数据库获取的数据量比pageView的最大容纳值大一，以判断当前pageView之后，是否还有数据需要显示。
			result = articleinfoDao.getDetailMessagesOfArticle(articleId, searchPattern, pageView.getFirstDataRef(), pageView.getMaxAllowedDataCount()+1 );
			
			if (logger.isDebugEnabled()) {
				logger.debug("Cache content is not found for pgView " + pageView.getID() + " , fetch the data from DB. " + result.size() + " data fetched.");
			}
			
			if (result.size() > 0) {
				dbDataCache.put(new Element(pageView.getID(), result ) );
				if (logger.isDebugEnabled()) {
					logger.debug("Cache content is set for pgView " + pageView.getID() + " , data size is " + result.size() );
				}
			}
		}
		
		pageView.loadData(result);
		return pageView;
	}

	@Override
	public void doSaveArticle(ArticleinfoDto articleinfo, ArticleDetailMessageDto detailMessage) {		
		articleinfoDao.doSaveArticle(articleinfo, detailMessage);
	}

	@Override
	public void doSaveReply(ArticleDetailMessageDto message, PageView oriPageView) {
		// 如果有新的内容产生，原来的PageView缓存需要清除。
		if (oriPageView != null) {
			dbDataCache.remove(oriPageView.getID());
		}
		articleinfoDao.doSaveReply(message);
	}

	@Override
	public void doChangeMessage(Long messageId, String summary, String summaryPicUrl, String content, String title, MyDateDTTM updateDateDTTM, String ip,Long userId, PageView oriPageView) {
		// 如果有新的内容产生，原来的PageView缓存需要清除。
		if (oriPageView != null) {
			dbDataCache.remove(oriPageView.getID());
		}
		articleinfoDao.doChangeMessage(messageId,summary,summaryPicUrl,content,title, updateDateDTTM,ip,userId);
	}

	@Override
	public ArticleDetailMessageDto getDetailMessage(Long messageId) {
		return articleinfoDao.getDetailMessage(messageId);
	}

	@Override
	public boolean doDeleteMessage(Long messageId,Long userId, PageView oriPageView) {
		// 如果有新的内容产生，原来的PageView缓存需要清除。
		if (oriPageView != null) {
			dbDataCache.remove(oriPageView.getID());
		}
		return articleinfoDao.doDeleteMessage(messageId,userId);
	}
	
}